import { getRandom, timeoutTask } from '../../../utils/index';
// import defaultContent from '../../../assets/constant/default';
const commentDanmu = [
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/sgVQIa15u0KPFI6mQNoNlsnBRvWTakvwnzCvjLuzG3M5wBuMczamdkHPcsA94gJx8s4QBpPLoIovt8GnxRE8R7Bz33CnZUfS/132',
    comment: '获得了一张普天同庆券',
    name:'李刚'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/et9q9FvjBBqypZHtuiaUC7Y66ZqAj46FmxBxBqlswjpyJkApKicn9zveseiaVTegSGrlPibWpZQamZygWpxJibgwaeA8vI45cX7tE/132',
    comment: '获得了一张普天同庆券',
    name:'『樂言』主持人·蒋帅'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/babzPZO8y1LmzwRZiavjw2R5jz1opicyR3EUAPR8cnUL7bXnCAvQTQXibZgBJK4hVhA8rlsY6h4yxSahsUkoXHVFa7fMppuOYEb/132',
    comment: '获得了一张普天同庆券',
    name:'中国鑫星婚礼团队创始人-申泽'
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqFfjkHXLmgdYTQG1RDdfZiclWIjiaic5NNKO6gCvZh1uMIDZDV0XiaWxriaEBo8bbvs29qbgXLjqfgyMQ/132',
    comment: '获得了一张功勋券',
    name:'吴大伟-爱联盟'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/Ke6AFV3xx80xZX3tsiaTLmH4ef31s6C7N3pfAicdnFUunY9rWMQuwAQqNBLSib7WNBJf44DH7zeRalia2AZlx3AFMpTtq7jnYzpL/132',
    comment: '获得了一张功勋券',
    name:'婚礼人-周周'
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erDOonhyVbbyJOiabbjOs6luCBFpm0Lqu4nVEw1vaNfaBJCjq2cZI6icrKXGFFHG0mb77kiaLEE8MyZg/132',
    comment: '获得了一张功勋券',
     name:'中国·鼎盛－阿郎'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/babzPZO8y1LmzwRZiavjw2WYmqaafgfKWMTpnenOuAV1FTxDzQgaZian74zPBqsTMfhVtmH5kzsBX4DK4QQDbsgmibTbLE8V6a6/132',
    comment: '获得了一张功勋券',
     name:'司·享&主持执行_天悦'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/babzPZO8y1JHSvFic6ooUtzlQicravu4L0tcq6JXicEcbicmbXdKiarJTBK5dDmIjZ7cPe9p2mLyztUIP3IrU3s8g3Q2iayyISECKG/132',
    comment: '获得了一张功勋券',
     name:'译匠〡本钒'
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTK2d9jfSKbs6G4jibzkWFIZtD5t9MY0fbJWYU5Qe3LwfeVCRJrOxQiaicKWbN0HXLnKwxlP7or8FZM2A/132',
    comment: '获得了一张功勋券',
     name:'主持人 李宏森'
  },
   {
    avator: 'http://thirdwx.qlogo.cn/mmopen/et9q9FvjBBoCuh6zJ9ibn3L4e9wlC6gcjdxUh78NotorSORC0iaFyATe43D8u2jCNNicIyaaJSxwsZX3vrrkOXl8w/132',
    comment: '获得了一张助力券',
     name:'无锡主持人-莫凡'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/sgVQIa15u0KPFI6mQNoNln7jNfRia2uQegpFKZz3XTxLMRJyFxcGu9y2DA6tW1oic5Nmf6Cf2ibAyGxQQCjWHRLJn0POx1MSzkg/132',
    comment: '获得了一张助力券',
     name:''
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEJWaamB8hLvsVUyk23ho81pL5x2ab7gv1nTUDV2Vear8RpWvbILLicCg2gJ6A15m7YPdiaPvMoibzElQ/132',
    comment: '获得了一张助力券',
     name:'徐大鹏--主持人音乐人'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/babzPZO8y1LmzwRZiavjw2RGgusmqxibXxd8AyJn9zvMd1Lu1IZ6oWW9nKG2X9uZWlH8WyEYIOj7Znp73GTpOic57uKRwTibuYZc/132',
    comment: '获得了一张普天同庆券',
     name:''
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erRAgXdaPiaXzYswNQfTexbh1Sn4UcMDQsHqFcIz3ZgoEQKtPXTibVs0KR6dSdabiayhOjlHu0LtY6WA/132',
    comment: '获得了一张助力券',
     name:'【锋尚】主持执行团队-阿汤'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/Q3auHgzwzM4a4FZnACB5uZ3pltibYKx8CKdJ0VDicvnFAsBicwdH9fehfwS838Qh7eOJs4DRxC21sPOeGfQOedH5Q/132',
    comment: '获得了一张普天同庆券',
     name:'陈建桥是个主持人'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/PiajxSqBRaEI2DF5rsXLWzNgNIuj0jK3vczcMGdibDNw1bYwX9KcnmPtKJ3NwYBubGoxEsZQOyTEgQ7n1I92tlPw/132',
    comment: '获得了一张功勋券',
     name:'邱先生。'
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erycJ6fxUe37X9487qiacUF1sYsgXvGXgMsgGtr3wy1UEw5QYPSVCcHdUXXE3V7jTafz042JwLTTOQ/132',
    comment: '获得了一张功勋券',
     name:'主持配音讲师李照'
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIk3GP5NzIUia3iahjXkgJTxNwfSFXic1libraEUAER4VaGlMoLm0qhGAMpYoIdkZiaQ2P8bHS4qiaeNEYA/132',
    comment: '获得了一张好友助力券',
     name:'『樂言』主持人·杨乐'
  },
  {
    avator: 'https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erXSKW5HwjMpstDmOq2IUqPSLUnnefOjtOVFRM4EKKGwdBhI7ibaMRibKNnmfrXvxClJot68KkYASibg/132',
    comment: '获得了一张好友助力券',
     name:'I Do 力揚'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/sgVQIa15u0ITaDnibuRUOegas8jUBsdN8h82gFhia0lJEIMrTXYAktIbfoLdd6jzqYibeEhXmPZY42Nt5jgf4LNTA/132',
    comment: '获得了一张好友助力券',
     name:'阿永-悟之聲「隽永留声」'
  },
  {
    avator: 'http://thirdwx.qlogo.cn/mmopen/et9q9FvjBBof3tAdBbs33Un4LvgiaKj5sia2YFeoy7hlnZeYiakuVTBLmVoZ9eFrGtkm8EKrKEyGryyIFAgFib2MuA/132',
    comment: '获得了一张好友助力券',
     name:'译匠 | 滕飛「西装 定制」'
  }
]
const danmusLen = commentDanmu.length;
let timer = undefined;
let indexArr = [];
Component({
  /**
   * 组件的属性列表
   */
  properties: {
  },

  /**
   * 组件的初始数据
   */
  data: {
    danmu: {
      avator: '',
      comment: '',
      moveDistance: 0,
      type: -1,
    },
  },
  lifetimes: {
    attached() {
      timer = undefined;
      indexArr = [];
      this.loopShowDanmu();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    loopShowDanmu() {
      if (timer) {
        return;
      }
      this.sendDanmu();
      timer = setInterval(() => {
        this.sendDanmu();
      }, 9000);
    },
    // 发送弹幕
    sendDanmu() {
      const danmuIndex = this.generateDanmuIndex();
      this.setData({
        ['danmu.avator']: commentDanmu[danmuIndex].avator,
        ['danmu.name']: commentDanmu[danmuIndex].name,
        ['danmu.comment']: commentDanmu[danmuIndex].comment,
        ['danmu.moveDistance']: 0,
        ['danmu.type']: getRandom(1, 4),
      });
      timeoutTask(() => {
        this.setData({
          ['danmu.moveDistance']: -(
            commentDanmu[danmuIndex].comment.length * 14 +
            500
          ),
        });
        // ;
      }, 500);
      timeoutTask(() => {
        this.setData({
          ['danmu.avator']: '',
          ['danmu.comment']: '',
          ['danmu.moveDistance']: 0,
          ['danmu.type']: -1,
        });
      }, 8500);
    },
    // 创建一个打乱顺序的索引数组
    generateIndexArr() {
      const tmpIndexArr = [];
      for (let i = 0; i < danmusLen; i += 1) {
        tmpIndexArr.push(i);
      }
      tmpIndexArr.sort((a, b) => {
        return Math.random() > 0.5 ? -1 : 1;
      });
      return tmpIndexArr;
    },
    // 弹幕索引
    generateDanmuIndex() {
      let tmpIndex = -1;
      if (indexArr.length > 0) {
        tmpIndex = indexArr.pop();
      } else {
        indexArr = this.generateIndexArr();
        tmpIndex = indexArr.pop();
      }
      return tmpIndex;
    },
    endLoop() {
      // ;
      clearInterval(timer);
      timer = undefined;
    },
  },
});
